﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using DTCash.Logger;
using Uranus.Service.FileService;

namespace Uranus.Service
{
    public class File
    {
        private static File file;

        private File()
        {

        }

        public static File GetFile()
        {
            if (file == null)
            {
                file = new File();

            }
            return file;
        }

        public bool UpFile(byte[] data, string filename)
        {
            //FileService 
            FileServiceSoapClient service = new FileServiceSoapClient();
            try
            {
                return service.FileStore(data, filename);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogError("上传文件异常 UpFile", $"filename:{filename}", ex);
                return false;
            }
            finally
            {
                service.Close();
            }

        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="stream">文件流</param>
        /// <param name="filename">文件路径</param>
        /// <returns></returns>
        public bool UpFile(Stream stream, string filename)
        {
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            return UpFile(bytes, filename);
        }

        public byte[] GetFile(string filename)
        {
            FileServiceSoapClient service = new FileServiceSoapClient();
            try
            {
                return service.GetFile(filename);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLogError("获取文件异常 GetFile", $"filename:{filename}", ex);
                return new byte[0];
            }
            finally
            {
                service.Close();
            }
        }
        
    }
}
